package org.yourscrum.persistence;

import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;

/**
 * @author Jettro Coenradie
 */
public abstract class RepositoryCallback<T> {
    private PersistenceManagerFactory persistenceManagerFactory;

    public RepositoryCallback(PersistenceManagerFactory persistenceManagerFactory) {
        this.persistenceManagerFactory = persistenceManagerFactory;
    }

    public T useRepository() {
        PersistenceManager pm = persistenceManagerFactory.getPersistenceManager();
        try {
            return doUseRepository(pm);
        } finally {
            pm.close();
        }
    }

    /*default*/
    abstract T doUseRepository(PersistenceManager pm);
}
